package cn.donzell.app.utils;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import cn.donzell.app.model.Identity;

public class IdCardGenerator {

	/**
	 * 生成身份证号
	 * 
	 * @return
	 */
	public static Identity generate(Integer areaCode, String yearBegin, String yearEnd, String sex) {
		Identity identity = new Identity();
		StringBuilder generater = new StringBuilder();
		generater.append(areaCode); // 区号

		String birthday = randomBirthday(yearBegin, yearEnd);// 年龄
		generater.append(birthday);
		identity.setBirthday(formatterDate(birthday));

		generater.append(randomCode()); // 两位随机数

		int randomSex = randomSex(sex);// 性别
		identity.setSex(randomSex % 2 == 0 ? "女" : "男");
		generater.append(randomSex);
		generater.append(calcTrailingNumber(generater.toString().toCharArray())); // 十八位
		identity.setIdCard(generater.toString());
		return identity;
	}

	private static String formatterDate(String date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		ParsePosition pos = new ParsePosition(0);
		Date birthday = formatter.parse(date, pos);
		formatter = new SimpleDateFormat("yyyy年MM月dd日");
		return formatter.format(birthday);
	}

	private static int getRandom(String yearBegin, String yearEnd) {
		Random random = new Random();
		int max = Integer.valueOf(yearEnd);
		int min = Integer.valueOf(yearBegin);
		int randomCode = random.nextInt(max) % (max - min + 1) + min;
		return randomCode;

	}

	private static String randomBirthday(String yearBegin, String yearEnd) {
		Calendar birthday = Calendar.getInstance();
		birthday.set(Calendar.YEAR, getRandom(yearBegin, yearEnd));
		birthday.set(Calendar.MONTH, (int) (Math.random() * 12));
		birthday.set(Calendar.DATE, (int) (Math.random() * 31));

		StringBuilder builder = new StringBuilder();
		builder.append(birthday.get(Calendar.YEAR));
		long month = birthday.get(Calendar.MONTH) + 1;
		if (month < 10) {
			builder.append("0");
		}
		builder.append(month);
		long date = birthday.get(Calendar.DATE);
		if (date < 10) {
			builder.append("0");
		}
		builder.append(date);
		return builder.toString();
	}

	/**
	 * 随机产生2位数
	 */
	private static String randomCode() {
		int code = (int) (Math.random() * 100);
		if (code < 10) {
			return "0" + code;
		} else {
			return "" + code;
		}
	}

	private static int randomSex(String sex) {
		int code = (int) (Math.random() * 9) + 1;
		if ("男".equals(sex)) {
			code = (int) (Math.random() * 8) + 1;
			return code % 2 == 0 ? code + 1 : code;
		} else if ("女".equals(sex)) {
			code = (int) (Math.random() * 8) + 1;
			return code % 2 == 0 ? code : code + 1;
		}
		return code;

	}

	private static char calcTrailingNumber(char[] chars) {
		if (chars.length < 17) {
			return ' ';
		}
		int[] c = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
		char[] r = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
		int[] n = new int[17];
		int result = 0;
		for (int i = 0; i < n.length; i++) {
			n[i] = Integer.parseInt(chars[i] + "");
		}
		for (int i = 0; i < n.length; i++) {
			result += c[i] * n[i];
		}
		return r[result % 11];
	}
}
